package org.zhxy.core.principal.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.zhxy.common.config.MapperFactory;
import org.zhxy.common.response.Result;
import org.zhxy.common.response.ResultCode;
import org.zhxy.common.utils.BeanMapUtils;
import org.zhxy.common.utils.Constants;
import org.zhxy.entity.principal.*;
import org.zhxy.entity.quality.Classes;

import java.util.List;
import java.util.Map;

import static org.zhxy.common.config.MapperFactory.schoolMapper;
import static org.zhxy.common.config.MapperFactory.subSchoolMapper;
import static org.zhxy.common.config.MapperFactory.departmentMapper;

@Service
@Transactional
public class SchoolService {

    public School getSchoolList() {
        return this.handleSchool(schoolMapper.selectOne(null));
    }

    public Result saveSchoolOrSubSchoolOrDepartment(Map<String, Object> map) throws Exception {
        switch (map.get(Constants.DB.TYPE).toString()){
            case Constants.CharacterNumber.ONE:
                School target = schoolMapper.selectOne(null);
                if(!StringUtils.isEmpty(target)) return new Result(ResultCode.SCHOOL_IS_EXIST);
                School school = BeanMapUtils.mapToBean(map, School.class);
                schoolMapper.insert(school);
                return Result.SUCCESS();
            case Constants.CharacterNumber.TWO:
                SubSchool subSchool = BeanMapUtils.mapToBean(map, SubSchool.class);
                subSchoolMapper.insert(subSchool);
                return Result.SUCCESS();
            case Constants.CharacterNumber.THREE:
                Department department = BeanMapUtils.mapToBean(map, Department.class);
                departmentMapper.insert(department);
                return Result.SUCCESS();
            default:
                return Result.FAIL();
        }
    }

    public Result updateSchoolOrSubSchoolOrDepartment(Map<String, Object> map) throws Exception {
        switch (map.get(Constants.DB.TYPE).toString()){
            case Constants.CharacterNumber.ONE:
                School school = BeanMapUtils.mapToBean(map, School.class);
                schoolMapper.updateById(school);
                return Result.SUCCESS();
            case Constants.CharacterNumber.TWO:
                SubSchool subSchool = BeanMapUtils.mapToBean(map, SubSchool.class);
                subSchoolMapper.updateById(subSchool);
                return Result.SUCCESS();
            case Constants.CharacterNumber.THREE:
                Department department = BeanMapUtils.mapToBean(map, Department.class);
                departmentMapper.updateById(department);
                return Result.SUCCESS();
            default:
                return Result.FAIL();
        }
    }

    public Result removeSchoolOrSubSchoolOrDepartment(Map<String, Object> map) {
        switch (map.get(Constants.DB.TYPE).toString()){
            case Constants.CharacterNumber.ONE:
                schoolMapper.deleteById(map.get(Constants.DB.ID).toString());
                return Result.SUCCESS();
            case Constants.CharacterNumber.TWO:
                subSchoolMapper.deleteById(map.get(Constants.DB.ID).toString());
                return Result.SUCCESS();
            case Constants.CharacterNumber.THREE:
                departmentMapper.deleteById(map.get(Constants.DB.ID).toString());
                return Result.SUCCESS();
            default:
                return Result.FAIL();
        }
    }

    public Result searchDepartmentBySchool() {
        List<School> schools = schoolMapper.selectList(null);
        if(!CollectionUtils.isEmpty(schools)){
            for (School school : schools) {
                this.handleSchool(school);
            }
        }
        return new Result(ResultCode.SUCCESS_NO_MESSAGE,schools);
    }

    protected School handleSchool(School school){
        if(!StringUtils.isEmpty(school)){
            QueryWrapper<SubSchool> subSchoolQueryWrapper = new QueryWrapper<>();
            subSchoolQueryWrapper.eq(Constants.DB.SCHOOL_ID,school.getId());
            List<SubSchool> subSchools = subSchoolMapper.selectList(subSchoolQueryWrapper);
            if(!CollectionUtils.isEmpty(subSchools)){
                for (SubSchool subSchool : subSchools) {
                    QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
                    departmentQueryWrapper.eq(Constants.DB.SUB_SCHOOL_ID,subSchool.getId());
                    List<Department> departments = departmentMapper.selectList(departmentQueryWrapper);
                    subSchool.setChildren(departments);
                }
            }
            school.setChildren(subSchools);
            return school;
        }
        return new School();
    }


    public List<School> searchCasVocationalList() {
        List<School> schools = MapperFactory.schoolMapper.selectList(null);
        if(org.zhxy.common.utils.CollectionUtils.checkCollection(schools)){
            schools.forEach(school -> {
                QueryWrapper<SubSchool> subSchoolQueryWrapper = new QueryWrapper<>();
                subSchoolQueryWrapper.eq(Constants.DB.SCHOOL_ID,school.getId());
                List<SubSchool> subSchools = MapperFactory.subSchoolMapper.selectList(subSchoolQueryWrapper);
                school.setChildren(subSchools);
                if(org.zhxy.common.utils.CollectionUtils.checkCollection(subSchools)){
                    subSchools.forEach(subSchool -> {
                        QueryWrapper<Vocational> vocationalQueryWrapper = new QueryWrapper<>();
                        vocationalQueryWrapper.eq(Constants.DB.SUB_SCHOOL_ID,subSchool.getId());
                        vocationalQueryWrapper.orderByDesc(Constants.DB.SORT_NUMBER);
                        List<Vocational> vocationalList = MapperFactory.vocationalMapper.selectList(vocationalQueryWrapper);
                        subSchool.setVocationalChildren(vocationalList);
                        if(org.zhxy.common.utils.CollectionUtils.checkCollection(vocationalList)){
                            vocationalList.forEach(vocational -> {
                                QueryWrapper<Stage> stageQueryWrapper = new QueryWrapper<>();
                                stageQueryWrapper.eq(Constants.DB.VOCATIONAL_ID,vocational.getId());
                                stageQueryWrapper.orderByDesc(Constants.DB.SORT_NUMBER);
                                List<Stage> stages = MapperFactory.stageMapper.selectList(stageQueryWrapper);
                                vocational.setChildren(stages);
                            });
                        }
                    });
                }
            });
        }
        return schools;
    }

    public List<School> searchCasClassesList() {
        List<School> schools = MapperFactory.schoolMapper.selectList(null);
        if(org.zhxy.common.utils.CollectionUtils.checkCollection(schools)){
            for (School school : schools) {
                QueryWrapper<SubSchool> subSchoolQueryWrapper = new QueryWrapper<>();
                subSchoolQueryWrapper.eq(Constants.DB.SCHOOL_ID,school.getId());
                List<SubSchool> subSchools = MapperFactory.subSchoolMapper.selectList(subSchoolQueryWrapper);
                school.setChildren(subSchools);
                if(org.zhxy.common.utils.CollectionUtils.checkCollection(subSchools)){
                    for (SubSchool subSchool : subSchools) {
                        QueryWrapper<Vocational> vocationalQueryWrapper = new QueryWrapper<>();
                        vocationalQueryWrapper.eq(Constants.DB.SUB_SCHOOL_ID,subSchool.getId());
                        vocationalQueryWrapper.orderByDesc(Constants.DB.SORT_NUMBER);
                        List<Vocational> vocationalList = MapperFactory.vocationalMapper.selectList(vocationalQueryWrapper);
                        subSchool.setVocationalChildren(vocationalList);
                        if(org.zhxy.common.utils.CollectionUtils.checkCollection(vocationalList)){
                            for (Vocational vocational : vocationalList) {
                                QueryWrapper<Stage> stageQueryWrapper = new QueryWrapper<>();
                                stageQueryWrapper.eq(Constants.DB.VOCATIONAL_ID,vocational.getId());
                                stageQueryWrapper.orderByDesc(Constants.DB.SORT_NUMBER);
                                List<Stage> stages = MapperFactory.stageMapper.selectList(stageQueryWrapper);
                                vocational.setChildren(stages);
                                if(org.zhxy.common.utils.CollectionUtils.checkCollection(stages)){
                                    for (Stage stage : stages) {
                                        QueryWrapper<Classes> classesQueryWrapper = new QueryWrapper<>();
                                        classesQueryWrapper.eq(Constants.DB.STAGE_ID,stage.getId());
                                        List<Classes> classes = MapperFactory.classesMapper.selectList(classesQueryWrapper);
                                        stage.setChildren(classes);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return schools;
    }
}
